package demo.game.chess.client.ui;

import demo.game.chess.common.ChessMoveMessage;
import demo.game.chess.common.ChessUndoMoveMessage;
import demo.game.chess.common.EnumChessColor;
import demo.messaging.client.core.IMessageEventListener;
import demo.messaging.client.core.MessageEvent;
import demo.messaging.client.core.MessageEvent.EnumMessageEventType;

public class OnlineChessUI extends AbstractChessUI implements
		IMessageEventListener {

	public OnlineChessUI(final EnumChessColor playerColor) {
		super(playerColor);

		getMoveListeners().add(new OnlineChessMoveListener(this));

	}

	@Override
	public void onMessageEvent(final MessageEvent event) {
		if (event.getType() == EnumMessageEventType.applicationDataReceived) {
			if (event.getMessage() instanceof ChessMoveMessage) {
				ChessMoveMessage msg = event.getMessage();
				this.confirmMove(msg.getMove());
				setEnabledUndo(true);
			} else if (event.getMessage() instanceof ChessUndoMoveMessage) {
				this.undoLastMove();
			}
		}
	}

}
